import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class GameWindow extends JFrame{

	private static int TIMER_HOP = 80;
	private static Dimension GAME_DIM = new Dimension(369,297);
	
	
	private static final String title = "Snake Master";
	private static JLabel info = new JLabel(" Score: "+0+"      ", SwingConstants.RIGHT);
	private static final Color background = Color.BLACK;
	private static Timer timer;
	private static GameDisplay gameDisplay = new GameDisplay(GAME_DIM);
	private static KeyListener keyListener = new Spyer(gameDisplay.getSnakeEngine());
	private static ActionListener actionListener = (ActionListener) keyListener;
	
	//menu
	
	private static final JMenuItem restart = new JMenuItem("Restart   [Ctrl+r]");
	private static final JMenuItem pause = new JMenuItem("Pause   [Space]");
	private static final JMenu menu1 = new JMenu("Menu");
	private static final JMenuItem looser = new JMenuItem("1 looser");
	private static final JMenuItem notbad = new JMenuItem("2 not so bad");
	private static final JMenuItem medium = new JMenuItem("3 medium");
	private static final JMenuItem expert = new JMenuItem("4 expert");
	private static final JMenuItem god = new JMenuItem("5 god style");
	private static final JMenu menu2 = new JMenu("Level");
	private static final JMenuBar menu = new JMenuBar();
	
	
	
	
	public GameWindow(){
		//finestra
		super(title);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.addKeyListener(keyListener);
		
		//menu
		restart.setActionCommand("restart");
		restart.addActionListener(actionListener);		
		pause.setActionCommand("pause");
		pause.addActionListener(actionListener);
		menu1.add(restart);
		menu1.add(pause);
		
		looser.setActionCommand("looser");
		looser.addActionListener(actionListener);
		menu2.add(looser);
		notbad.setActionCommand("notbad");
		notbad.addActionListener(actionListener);
		menu2.add(notbad);
		medium.setActionCommand("medium");
		medium.addActionListener(actionListener);
		menu2.add(medium);
		expert.setActionCommand("expert");
		expert.addActionListener(actionListener);
		menu2.add(expert);
		god.setActionCommand("god");
		god.addActionListener(actionListener);
		menu2.add(god);
		
		menu.add(menu1);
		menu.add(menu2);
				
		this.setJMenuBar(menu);
		
		
		Container cp = this.getContentPane();
		cp.setLayout(new BorderLayout());
		cp.setBackground(background);

		//label punteggio
		info.setBackground(background);
		info.setForeground(Color.GRAY);
		cp.add(info, BorderLayout.NORTH);
		
		//fiestra gioco
		gameDisplay.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		gameDisplay.setBackground(background);
		gameDisplay.setPreferredSize(GAME_DIM);
		cp.add(gameDisplay, BorderLayout.CENTER);
		
		//timer
		timer = new Timer(TIMER_HOP,actionListener);
		timer.setActionCommand("timer");
		timer.start();
		
		this.pack();
		this.setVisible(true);
	}
	
	public static void refreshGameDisplay(){
		gameDisplay.repaint();
	}
	public static void refreshPoint(){
		int score = gameDisplay.getSnakeEngine().getSnakeLong();
		timer.setDelay(TIMER_HOP + (score/10));
		//System.out.println("timer hop = "+timer.getDelay());
		info.setText(" Score: "+score+"      ");
	}
		

	public static void startTimer(){
		timer.start();
	}
	public static void stopTimer(){
		timer.stop();
	}
	public static void setTimerHop(int hop){
		TIMER_HOP = hop;
		timer.setDelay(hop);
	}
	
	public static void main(String[] args) {
		new GameWindow();		
	}
	
}